<?php
  require_once('db_connection.php');
  require_once('includes/header.php');

  $id = isset($_GET['id'])?$_GET['id']:0;
  $search = $s_where = '';

# uncomment following code for server variables
//pr($_SERVER);
#$qs = isset($_SERVER['QUERY_STRING'])?'?'.$_SERVER['QUERY_STRING']:'';

  pr($_GET);
  pr($_POST);
  /*********** SEARCH **************/
  if(isset($_GET['search']) && !empty($_GET['search'])){
    $search = trim($_GET['search']);
    $s_where = "`name` LIKE '%$search%' OR `email` LIKE '%$search%'";
  }
  /*********************************/
  // want to delete by checkbox
  if(isset($_POST['chk'])){
    $ids = implode(',',$_POST['chk']);
    pr($ids);
    $chk_where = "`id` IN($ids)";
    $delRows = delete('products',$chk_where);
    pr($delRows);
  }
  if(isset($_GET['mode']) && $_GET['mode']=='del') {

    $deletedRows = deleteById('products',$id);
    pr($deletedRows);
  }


  // if set in URL then update
  if(isset($_GET['status']) && isset($_GET['id'])) {
    $url_status = ($_GET['status'] == 1) ? 0 : 1;

    /******* start query  *********/
    $data = array('status'=>$url_status);
    $u_where = "`id`= '$id'";
    $updated_rows = update('products',$data,$u_where);
    /******* end query  *********/
    if($updated_rows == 1){
      echo "<h4>Status changed successfully..</h4>";
    }
  }

  //////////////////////////////
  $curPage = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 1;
  $totalrecords = countAll('products',$s_where);
  $rpp = 2;// Record Per Page
  $limit = ($curPage-1)*$rpp;
pr($totalrecords);
  $limitClause = "$limit,$rpp";
  $totalPages = ceil( $totalrecords / $rpp );
  /******* SORTING ***/
  $field = isset($_GET['field']) && !empty($_GET['field']) ? $_GET['field'] : 'added_date';
  $order = isset($_GET['order']) && !empty($_GET['order']) ? $_GET['order'] : 'DESC';
  $site_orders = array('asc','desc');
  if(!in_array(strtolower($order), $site_orders)){
    $order = 'desc';
  }
  $newOrder = ($order=='asc')?'desc':'asc';

  /******* SORTING ***/
  $result  = fetchAll('products',$s_where,"`$field` $order",$limitClause);
  //$result  = fetchAll('products','','',$limitClause);
  //////////////////////////////
  $selected_records = mysql_num_rows($result);
    //echo "<h4>TOTAL : $totalrecords </h4>";
    //echo "<h4>On This Page :$selected_records </h4>";

?>

<div class="container" id="wrap">


  <div class="container">
  <form role="search" class="navbar-form navbar-left">
              <div class="form-group">
                <input type="text" placeholder="Search" name="search" placeholder="Search Here.." class="form-control">
              </div>
              <button class="btn btn-default" type="submit">Search</button>
            </form>
  </div>

      <ul class="nav nav-pills">
        <li class="active"><a href="#">TOTAL <span class="badge"><?=$totalrecords?></span></a></li>
        <li><a href="#">On This Page <span class="badge"><?=$selected_records?></span></a></li>
      </ul>

<a href="add_product.html">Add New</a>
<form action="" method="post">
  <table class="table table-striped table-bordered table-hover">
    <thead>
      <tr>
        <th><input type="checkbox" id="chk_pid" name="c_all" value="c_parent" /></th>
        <th><a href="?page=<?=$curPage?>&order=<?=$newOrder?>&field=id&search=<?=$search?>">ID</a></th>
        <th>NAME</th>
        <th>Image</th>
        <th>ADDED DATE</th>
        <th><a href="?page=<?=$curPage?>&order=<?=$newOrder?>&field=status&search=<?=$search?>">STATUS</a></th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
  <?php
    while($ans = mysql_fetch_assoc($result)){
      $status = getStatus($ans['status']);
      $url = "?id=$ans[id]&status=$ans[status]&page=$curPage&search=$search";
      $status = '<a href="'.$url.'">'.$status.'</a>';
  ?>
      <tr>
        <td><input type="checkbox" class="chk" name="chk[]" value="<?=$ans['id']?>" /></td>
        <td><a href="show_user.php?id=<?=$ans['id']?>"><?=$ans['id']?></a></td>
        <td><?=$ans['name']?></td>
        <td><img height="90" width="120" alt="<?=$ans['image']?>" title="<?=$ans['image']?>" src="p_images/<?=$ans['image']?>"></td>
        <td><?=date('d-M-Y h:i:s a',strtotime($ans['added_date']))?></td>
        <td><?=$status?></td>
        <td><a href="?mode=del&id=<?=$ans['id']?>&page=<?=$curPage?>&search=<?=$search?>" onClick="return confirm('Are you sure to delete?');"><i class="fa fa-trash-o fa-2x"></i></a>&nbsp;/&nbsp;<a href="edit_product.php?id=<?=$ans['id']?>" ><i class="fa fa-edit fa-2x"></i></a></td>
      </tr>
  <?php
      //print_r($ans);
    }
  ?>


    </tbody>
  </table>
  <input type="submit" class="btn btn-danger" value="Delete All" id="delAllId" name="delAll" />  <br>
</form>

  <ul class="pagination pagination-lg">
    <li><a href="#">&laquo;</a></li>
  <?php
  for ($i = 1; $i <= $totalPages ; $i++)
  {
    if($i==$curPage){
  ?>

    <li class="active"><a href="#"><?=$i?> <span class="sr-only">(current)</span></a></li>
  <?php
    } else {
  ?>


    <li><a href="?page=<?=$i?>&order=<?=$order?>&field=<?=$field?>&search=<?=$search?>"><?=$i?></a></li>
  <?php
    }//else
  }
  ?>

    <li><a href="#">&raquo;</a></li>
  </ul>


</div><!---end #wrap-->


<?php
require_once("includes/footer.php");

?>


